package com.netcracker.studportal.server.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.netcracker.studportal.server.dao.ProfessorDAO;
import com.netcracker.studportal.server.model.Exam;
import com.netcracker.studportal.server.model.Professor;
import com.netcracker.studportal.server.util.HibernateUtil;

public class ProfessorDAOImpl implements ProfessorDAO {
	private static final Logger LOG = Logger.getLogger(ProfessorDAOImpl.class);

	@Override
	public void Add(Professor prof) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(prof);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public void Update(Professor prof) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(prof);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Professor getById(Long id) throws Exception {
		Session session = null;
		Professor prof = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			prof = (Professor) session.load(Professor.class, id);
			Hibernate.initialize(prof.getUniversity());
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();

			}
		}
		return prof;
	}

	@Override
	public void Delete(Professor prof) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(prof);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}

	}

	@Override
	public Collection<Professor> getAll() throws Exception {
		Session session = null;
		List<Professor> prof = new ArrayList<Professor>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			prof = session.createCriteria(Professor.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return prof;
	}

	@Override
	public Professor getProfessorsByExam(Exam ex) throws Exception {
		Session session = null;
		Professor prof = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ex = (Exam) session.load(Exam.class, ex.getId());
			prof = ex.getProfessor();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return prof;
	}

	@Override
	public Collection<Professor> getProfByUniversity(Long univ_id) throws Exception {
		// TODO Auto-generated method stub
		Session session = null;
		List<Professor> list;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			list = (List<Professor>) session
					.createSQLQuery(
							"select * from professors where univ_id=" + univ_id)
					.addEntity(Professor.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return list;
	}
		
	/**
	 * Realization of singleton.
	 */
	private ProfessorDAOImpl() {}
	private static ProfessorDAO instance;
	public static ProfessorDAO getInstance() {
		if (instance == null) {
			instance = new ProfessorDAOImpl();
		}
		return instance;
	}

}
